class Solution:
    _MOD = 10 ** 9 + 7

    def uniqueLetterString(self, s: str) -> int:
        idx_lst = [[] for _ in range(26)]

        for i, ch in enumerate(s):
            idx_lst[ord(ch) - 65].append(i)

        ans = 0

        for idx in idx_lst:
            if idx:
                idx = [-1] + idx + [len(s)]
                for i in range(1, len(idx) - 1):
                    ans += (idx[i + 1] - idx[i]) * (idx[i] - idx[i - 1])

        return ans % self._MOD


if __name__ == "__main__":
    print(Solution().uniqueLetterString("ABC"))  # 10
    print(Solution().uniqueLetterString("ABA"))  # 8
    print(Solution().uniqueLetterString("LEETCODE"))  # 92
